<template>
  <div>
    <el-card class="box-card">
      <div class="BigDiv">
        <el-input class="ElInput" v-model="monthCardCode"></el-input>
        <el-button icon="el-icon-search" type="primary" @click="getMonthCard()"></el-button>
        <el-button type="success" @click="toAddMonthCard()">添加</el-button>
        <!--表格-->
        <el-table
            :data="tableData"
            height="250"
            border
            style="margin-top: 10px;width: 100%">
          <el-table-column
              prop="monthCardCode"
              label="月卡编号"
              width="180">
          </el-table-column>
          <el-table-column
              prop="monthCardName"
              label="持卡人姓名"
              width="180">
          </el-table-column>
          <el-table-column
              prop="createTime"
              label="月卡办理时间">
          </el-table-column>
          <el-table-column
              prop="monthCardPlateNumber"
              label="绑定车牌">
          </el-table-column>
          <el-table-column
              prop="monthCardTime"
              label="月卡有效天数">
          </el-table-column>
          <el-table-column
              prop="monthCardState"
              label="月卡状态 "
              width="180">
            <template slot-scope="scope">
              <el-switch
                  style="margin-left: 50px"
                  @change="updateStatus(scope.row)"
                  v-model="scope.row.monthCardState"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  :active-value="0"
                  :inactive-value="1">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column
              prop="updateTime"
              label="修改时间">
          </el-table-column>
          <el-table-column
              fixed="right"
              label="操作">
            <template slot-scope="scope">
              <el-button type="primary" @click="toUpdate(scope.row)">修改</el-button>
              <el-button type="warning" @click="delMonthCard(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--表格-->
      </div>
    </el-card>
    <!--分页-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page=pageRquset.pageNum
        :page-sizes="[5, 10, 20, 30]"
        :page-size=pageRquset.pageSize
        layout="total, sizes, prev, pager, next, jumper"
        :total=total>
    </el-pagination>
    <!--分页-->
    <!-- 增改弹窗 -->
    <!-- Form -->
    <el-dialog title="增改"
               :visible.sync="dialogFormVisible"
               :before-close="closeExpertFormDialog">
      <el-form :model="monthForm" ref="ruleForm">
        <el-form-item label="持卡人姓名">
          <el-input v-model="monthForm.monthCardName"></el-input>
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="monthForm.monthCardPhone"></el-input>
        </el-form-item>
        <el-form-item label="绑定车牌号">
          <el-input v-model="monthForm.monthCardPlateNumber"></el-input>
        </el-form-item>
        <el-form-item label="办理人">
          <el-input v-model="monthForm.monthCardTransactor"></el-input>
        </el-form-item>
        <el-form-item label="有效日期">
          <el-input v-model="monthForm.monthCardEndTime"  type="date"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-input v-model="monthForm.monthCardState"></el-input>
        </el-form-item>
        <el-form-item label="费用">
          <el-input v-model="monthForm.monthCardNum"></el-input>
        </el-form-item>
        <el-form-item label="月卡有效天数">
          <el-input v-model="monthForm.monthCardTime"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="monthForm.monthCardBack"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdateMonth('insert')">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 增改弹窗 -->
    <!-- Form -->
  </div>
</template>

<script>
export default {
  /*Data*/
  data() {
    return {
      monthCardCode:'',
      pageRquset: {
        pageSize: 5,
        pageNum: 1
      },
      dialogTableVisible: false,
      dialogFormVisible: false,
      currentPage2: 1,
      total:0,
      input: '',
      tableData: [],
      monthForm:{
        id:0,
        monthCardCode:'',
        monthCardName:'',
        monthCardPhone:'',
        monthCardPlateNumber:'',
        monthCardNum:'',
        monthCardPayment:'',
        monthCardTransactor:'',
        monthCardStartDate:'',
        monthCardEndTime:'',
        monthCardTime:'',
        createTime:'',
        updateTime:'',
        monthCardBack:'',
        monthCardState:'',
        monthCardDelFlag:'',
      }
    }
  },
  /*自动挂载*/
  mounted() {
    this.getMonthCard()
  },
  /*函数*/
  methods:{
    /*月卡全查*/
    getMonthCard () {
      debugger
      this.$axios.get('/memberLwh/month/getMonthCard', {
        params: {
          monthCarCode: this.monthCarCode,
          pageSize: this.pageRquset.pageSize,
          pageNum: this.pageRquset.pageNum
        }
      }).then(res => {
        this.tableData = res.data.obj.content
        this.total = res.data.obj.total
      }).catch(error => {
        console.log(error)
      })
    },

    // 修改的回显
    toUpdate (monthCard) {
      this.monthForm = monthCard
      this.dialogFormVisible = true
    },
    /*打开添加窗口*/
    toAddMonthCard(){
      this.dialogFormVisible = true
      this.monthForm = {
        id: 0
      }
    },
    // 添加点击X关闭是清空from表单数据
    closeExpertFormDialog (done) {
      this.$refs.ruleForm.resetFields()
      done()// done 用于关闭 Dialog
      this.monthForm = {}
    },
    // 添加弹窗里的取消按钮 点击取消是清空from表单数据
    resetForm (formName) {
      this.$refs[formName].resetFields()
      this.dialogFormVisible = false
      this.monthForm = {}
    },
    /*分页*/
    handleSizeChange (val) {
      this.pageRquset.pageSize = val
      this.getMonthCard()
    },
    handleCurrentChange (val) {
      this.pageRquset.pageNum = val
      this.getMonthCard()
    },
    updateStatus(data) {
      this.$axios.post('memberLwh/month/saveOrUpdateMonth',data).then(res => {
        if (res.data.code === 200){
          if (data.monthCardState === 0) {
            this.MyMessage('启用成功','success')
          }
          if (data.monthCardState === 1) {
            this.MyMessage('停用成功','success')
          }
          this.list()
        } else {
          this.MyMessage(res.data.obj,'info')
        }
      })
    },
    // 月卡增改
    saveOrUpdateMonth (formName) {
      debugger
      // const formData = new FormData()
      this.$axios.post('/memberLwh/month/saveOrUpdateMonth', this.monthForm
      ).then(res => {
        if (res.data.code === 200) {
          this.dialogFormVisible = false
          this.MyMessage("增改成功","sussess")
          this.monthForm = {
            id:0,
            monthCardCode:'',
            monthCardName:'',
            monthCardPhone:'',
            monthCardPlateNumber:'',
            monthCardNum:'',
            monthCardPayment:'',
            monthCardTransactor:'',
            monthCardStartDate:'',
            monthCardEndTime:'',
            monthCardTime:'',
            createTime:'',
            updateTime:'',
            monthCardBack:'',
            monthCardState:'',
            monthCardDelFlag:'',
          }
          this.getMonthCard()
        } else {
          this.MyMessage('增改失败', 'warning')
          this.insertLog("月卡增改")
          this.getMonthCard()
        }
      })
    },
    /*月卡删除*/
    delMonthCard (id) {
      this.$confirm('是否删除这条数据', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.delete('/memberLwh/month/delMonthCard', {
          params: {
            id: id
          }
        }).then(res => {
          if (res.data.code === 200) {
            this.MyMessage('删除成功', 'sussess')
            this.getMonthCard()
          } else {
            this.MyMessage('删除失败', 'warning')
          }
        }).catch(res => {
          this.insertLog("月卡删除")
          this.MyMessage('删除失败,请联系管理员', 'error')
        })
      }).catch(() => {
        this.MyMessage('已取消删除', 'info')
      })
    },
    /*提示框*/
    MyMessage (msg, type) {
      this.$notify({
        showClose: true,
        message: msg,
        type: type
      })
    },
  }
}
</script>

<style>
*{
  margin: 0;
  padding: 0;
}
.ElInput{
  width: 200px;
}
.box-card{
  height: 820px;
  margin: 10px;
}
</style>